home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / actionfssm.lha / partinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-15  |  5.8 KB  |  209 lines

  1. /*
  2. ** PartInfo.c - obtain device and geometry data pertaining to a partition
  3. ** Copyright © 1993, 1994 by Ralph Babel, Falkenweg 3, D-65232 Taunusstein, FRG
  4. ** all rights reserved - alle Rechte vorbehalten
  5. **
  6. ** 09-Jun-1993 created
  7. ** 07-Jul-1993 fixed memcpy() call
  8. ** 24-Feb-1994 fixed Forbid()/Permit()
  9. */
  10.  
  11. /*** included files ***/
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <dos/dos.h>
  16. #include <dos/dosextens.h>
  17. #include <dos/filehandler.h>
  18. #include <clib/macros.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. /*** constants ***/
  25.  
  26. #define ACTION_GET_DISK_FSSM  4201
  27. #define ACTION_FREE_DISK_FSSM 4202
  28.  
  29. /*** types ***/
  30.  
  31. struct PartInfo
  32.  {
  33.  char *deviceName;      /* OpenDevice()'s A0 parameter */
  34.  ULONG deviceUnit;      /* OpenDevice()'s D0 parameter */
  35.  ULONG deviceFlags;     /* OpenDevice()'s D1 parameter */
  36.  ULONG bytesPerSector;  /* partition geometry */
  37.  ULONG sectorsPerBlock; /* partition geometry */
  38.  ULONG blocksPerTrack;  /* partition geometry */
  39.  ULONG surfaces;        /* partition geometry */
  40.  ULONG lowCylinder;     /* partition geometry */
  41.  ULONG highCylinder;    /* partition geometry */
  42.  ULONG reservedBlocks;  /* concession to default filesystem */
  43.  ULONG bootBlocks;      /* for Install-like programs */
  44.  ULONG memoryType;      /* safe io_Data default */
  45.  ULONG addressMask;     /* io_Data restriction */
  46.  ULONG maxTransfer;     /* io_Length restriction */
  47.  ULONG defaultDosType;  /* Format()'s default D3 parameter */
  48.  };
  49.  
  50. /*** code section ***/
  51.  
  52. void __regargs __chkabort(void) {} /* disable SAS/C Ctrl-C checking */
  53.  
  54. static LONG doStdPkt(struct StandardPacket *sp, struct MsgPort *mp, LONG *pIoErr)
  55.  {
  56.  struct MsgPort *rp;
  57.  
  58.  rp = &((struct Process *)FindTask(NULL))->pr_MsgPort;
  59.  
  60.  sp->sp_Msg.mn_Node.ln_Name = (char *)&sp->sp_Pkt;
  61.  sp->sp_Pkt.dp_Link         = &sp->sp_Msg;
  62.  sp->sp_Pkt.dp_Port         = rp;
  63.  
  64.  PutMsg(mp, &sp->sp_Msg);
  65.  (void)WaitPort(rp);
  66.  (void)GetMsg(rp); /* assumes that no other packets are pending */
  67.  
  68.  *pIoErr = sp->sp_Pkt.dp_Res2;
  69.  
  70.  return sp->sp_Pkt.dp_Res1;
  71.  }
  72.  
  73. static struct FileSysStartupMsg *getDiskFssm(struct MsgPort *mp)
  74.  {
  75.  LONG ioErr;
  76.  struct FileSysStartupMsg *fssm;
  77.  char SP[sizeof(struct StandardPacket) + 3];
  78.  struct StandardPacket *sp = (struct StandardPacket *)((ULONG)(SP + 3) & ~3);
  79.  
  80.  sp->sp_Pkt.dp_Type = ACTION_GET_DISK_FSSM;
  81.  
  82.  fssm = (struct FileSysStartupMsg *)doStdPkt(sp, mp, &ioErr);
  83.  
  84.  /*
  85.  ** If need be, try alternative scheme if and only if fssm
  86.  ** equals NULL and ioErr equals ERROR_ACTION_NOT_KNOWN.
  87.  */
  88.  
  89.  return fssm;
  90.  }
  91.  
  92. static void freeDiskFssm(struct MsgPort *mp, struct FileSysStartupMsg *fssm)
  93.  {
  94.  LONG ioErr;
  95.  char SP[sizeof(struct StandardPacket) + 3];
  96.  struct StandardPacket *sp = (struct StandardPacket *)((ULONG)(SP + 3) & ~3);
  97.  
  98.  sp->sp_Pkt.dp_Type = ACTION_FREE_DISK_FSSM;
  99.  sp->sp_Pkt.dp_Arg1 = (LONG)fssm;
  100.  
  101.  (void)doStdPkt(sp, mp, &ioErr);
  102.  }
  103.  
  104. static struct PartInfo *getPartInfo(const char *name)
  105.  {
  106.  struct MsgPort *mp;
  107.  struct FileSysStartupMsg *fssm;
  108.  struct PartInfo *pi;
  109.  struct DosEnvec *de, DE;
  110.  char *deviceName;
  111.  size_t tableSize;
  112.  
  113.  pi = NULL;
  114.  
  115.  if((mp = DeviceProc((char *)name)) != NULL)
  116.   {
  117.   if((fssm = getDiskFssm(mp)) != NULL)
  118.    {
  119.    deviceName = 1 + (char *)BADDR(fssm->fssm_Device);
  120.  
  121.    if((pi = AllocMem((ULONG)(sizeof(struct PartInfo) + strlen(deviceName) + 1), 0)) != NULL)
  122.     {
  123.     pi->deviceName  = strcpy((char *)pi + sizeof(struct PartInfo), deviceName);
  124.     pi->deviceUnit  = fssm->fssm_Unit;
  125.     pi->deviceFlags = fssm->fssm_Flags;
  126.  
  127.     DE.de_BufMemType  = MEMF_CHIP | MEMF_PUBLIC;
  128.     DE.de_MaxTransfer = 0x7fffffff;
  129.     DE.de_Mask        = 0xfffffffe;
  130.     DE.de_DosType     = 0x444f5300;
  131.     DE.de_BootBlocks  = 2;
  132.  
  133.     de        = BADDR(fssm->fssm_Environ);
  134.     tableSize = (MIN(de->de_TableSize, DE_BOOTBLOCKS) + 1) * sizeof(ULONG);
  135.  
  136.     Forbid();
  137.     (void)memcpy(&DE, de, tableSize);
  138.     Permit();
  139.  
  140.     pi->bytesPerSector  = DE.de_SizeBlock * BYTESPERLONG;
  141.     pi->sectorsPerBlock = DE.de_SectorPerBlock;
  142.     pi->blocksPerTrack  = DE.de_BlocksPerTrack;
  143.     pi->surfaces        = DE.de_Surfaces;
  144.     pi->lowCylinder     = DE.de_LowCyl;
  145.     pi->highCylinder    = DE.de_HighCyl;
  146.     pi->reservedBlocks  = DE.de_Reserved;
  147.     pi->bootBlocks      = DE.de_BootBlocks;
  148.     pi->memoryType      = DE.de_BufMemType;
  149.     pi->addressMask     = DE.de_Mask;
  150.     pi->maxTransfer     = DE.de_MaxTransfer;
  151.     pi->defaultDosType  = DE.de_DosType;
  152.     }
  153.  
  154.    freeDiskFssm(mp, fssm);
  155.    }
  156.   }
  157.  
  158.  return pi;
  159.  }
  160.  
  161. static void freePartInfo(struct PartInfo *pi)
  162.  {
  163.  FreeMem(pi, (ULONG)(sizeof(struct PartInfo) + strlen(pi->deviceName) + 1));
  164.  }
  165.  
  166. /*** entry point ***/
  167.  
  168. int main(int argc, char *argv[])
  169.  {
  170.  int result;
  171.  struct PartInfo *pi;
  172.  
  173.  result = RETURN_FAIL;
  174.  
  175.  if(argc == 2)
  176.   {
  177.   result = RETURN_ERROR;
  178.  
  179.   if((pi = getPartInfo(argv[1])) != NULL)
  180.    {
  181.    result = RETURN_OK;
  182.  
  183.    printf("deviceName      = \"%s\"\n", pi->deviceName);
  184.    printf("deviceUnit      = %ld\n",    pi->deviceUnit);
  185.    printf("deviceFlags     = %ld\n",    pi->deviceFlags);
  186.    printf("bytesPerSector  = %ld\n",    pi->bytesPerSector);
  187.    printf("sectorsPerBlock = %ld\n",    pi->sectorsPerBlock);
  188.    printf("blocksPerTrack  = %ld\n",    pi->blocksPerTrack);
  189.    printf("surfaces        = %ld\n",    pi->surfaces);
  190.    printf("lowCylinder     = %ld\n",    pi->lowCylinder);
  191.    printf("highCylinder    = %ld\n",    pi->highCylinder);
  192.    printf("reservedBlocks  = %ld\n",    pi->reservedBlocks);
  193.    printf("bootBlocks      = %ld\n",    pi->bootBlocks);
  194.    printf("memoryType      = $%08lx\n", pi->memoryType);
  195.    printf("addressMask     = $%08lx\n", pi->addressMask);
  196.    printf("maxTransfer     = $%08lx\n", pi->maxTransfer);
  197.    printf("defaultDosType  = $%08lx\n", pi->defaultDosType);
  198.  
  199.    freePartInfo(pi);
  200.    }
  201.   else
  202.    fprintf(stderr, "Unable to obtain partition information.\n");
  203.   }
  204.  else
  205.   fprintf(stderr, "Usage: %s <devicename>\n", argv[0]);
  206.  
  207.  return result;
  208.  }
  209.